package com.ln.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.ln.model.DatasourceConfigEntity;
import com.ln.service.DatasourceConfigService;
import com.ln.utils.Result;
import com.ln.utils.PageUtils;


/**
 * 连接数据库信息
 * 
 * @author lining
 * @email 1501911035@qq.com
 * @date 2017-04-01 10:50:15
 */
@Controller
@RequestMapping("/datasourceconfig")
public class DatasourceConfigController {
	@Resource
	private DatasourceConfigService datasourceConfigService;
	
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("datasourceconfig:list")
	public Result list(Integer page, Integer limit){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//查询列表数据
		List<DatasourceConfigEntity> datasourceConfigList = datasourceConfigService.queryList(map);
		int total = datasourceConfigService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(datasourceConfigList, total, limit, page);
		
		map.put("page", pageUtil);
		return Result.success(map);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{dcId}")
	@RequiresPermissions("datasourceconfig:info")
	public Result info(@PathVariable("dcId") Integer dcId){
		DatasourceConfigEntity datasourceConfig = datasourceConfigService.queryObject(dcId);
		Map<String, Object> map = new HashMap<>();
		map.put("datasourceConfig", datasourceConfig);
		return Result.success(map);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("datasourceconfig:save")
	public Result save(@RequestBody DatasourceConfigEntity datasourceConfig){
		datasourceConfigService.save(datasourceConfig);
		
		return Result.success();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("datasourceconfig:update")
	public Result update(@RequestBody DatasourceConfigEntity datasourceConfig){
		datasourceConfigService.update(datasourceConfig);
		
		return Result.success();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("datasourceconfig:delete")
	public Result delete(@RequestBody Integer[] dcIds){
		datasourceConfigService.deleteBatch(dcIds);
		
		return Result.success();
	}
	
}
